package org.mypro.front;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mypro.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.util.UrlPathHelper;


public class UserLoginInterceptors implements HandlerInterceptor {

	private UrlPathHelper urlPathHelper = new UrlPathHelper();

	private static final Log logger = LogFactory.getLog(UserLoginInterceptors.class);

	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3) throws Exception {

	}

	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3) throws Exception {

	}

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object object) throws Exception {
		logger.debug("开始验证用户登录信息......。");
		User signInUserEntity = (User) request.getSession().getAttribute("LOGIN_USER");
		String contextPath = request.getContextPath();
		logger.debug("訪問地址:" + urlPathHelper.getRequestUri(request));
		if(urlPathHelper.getRequestUri(request).contains("login.action") 
				|| urlPathHelper.getRequestUri(request).contains("regedit.action")
				|| urlPathHelper.getRequestUri(request).contains("loginout.action")){
			return true;
		}
		if (signInUserEntity != null) {
			return true;
		} else {
			String loginPage = contextPath + "/" + "login.action";
			response.sendRedirect(loginPage);
			return false;
		}
	}

}
